package com.bell.atensettings;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import org.apache.http.conn.util.InetAddressUtils;

import com.bell.atensettings.R;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener, OnFocusChangeListener
{
    private Context mContext;
    private TextView mTitleView;
    private RelativeLayout mLayouts[] = new RelativeLayout[4];
    private NetStatReceiver netStatReceiver;

    private void initView()
    {
        mTitleView = (TextView) findViewById(R.id.setting_title);
        mLayouts[0] = (RelativeLayout) findViewById(R.id.network_settings);
        mLayouts[1] = (RelativeLayout) findViewById(R.id.display_setting);
        mLayouts[2] = (RelativeLayout) findViewById(R.id.adv_settings);
        mLayouts[3] = (RelativeLayout) findViewById(R.id.aboutus);

        mTitleView.setText(getResources().getString(R.string.app_name));
        for (int index = 0; index < 4; index++) {
            mLayouts[index].setOnFocusChangeListener(this);
            mLayouts[index].setOnClickListener(this);
        }

        ((TextView) findViewById(R.id.tv_ipaddr)).setText(getLocalIpAddress());
    }

    private String getLocalIpAddress()
    {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()
                            && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())
                            && !inetAddress.getHostAddress().toString().equals("null")
                            && inetAddress.getHostAddress() != null) {
                        return inetAddress.getHostAddress().toString();
                    }
                }
            }
        } catch (SocketException ex) {
            Log.e("WifiPreference IpAddress", ex.toString());
        }
        return null;
    }

    @Override
    protected void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);
        mContext = this;
        netStatReceiver = new NetStatReceiver();

        setContentView(R.layout.system_setting);
        registerReceiver(this.netStatReceiver, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
        initView();
    }

    @Override
    protected void onDestroy()
    {
        super.onDestroy();
        unregisterReceiver(netStatReceiver);
    }

    @Override
    protected void onPause()
    {
        super.onPause();
    }

    @Override
    protected void onResume()
    {
        super.onResume();
    }

    @Override
    protected void onStart()
    {
        super.onStart();
    }

    @Override
    public void onClick(View view)
    {
        Intent intent = null;
        switch (view.getId()) {
        // 网络设置
            case R.id.network_settings:
                intent = new Intent(this, NetWorkSettings.class);
                intent.putExtra("title", getResources().getString(R.string.network_settings));
                startActivity(intent);
                break;
            // 显示设置
            case R.id.display_setting:
                intent = new Intent(this, DisplaySettings.class);
                intent.putExtra("title", getResources().getString(R.string.display_settings));
                startActivity(intent);
                break;
            // 高级设置
            case R.id.adv_settings:
                intent = new Intent(this, FunctionsSettings.class);
                intent.putExtra("title", getResources().getString(R.string.adv_settings));
                startActivity(intent);
                break;
            // 系统信息
            case R.id.aboutus:
                intent = new Intent(this, AboutSettings.class);
                intent.putExtra("title", getResources().getString(R.string.about_system));
                startActivity(intent);
                break;
            default:
                break;
        }
        overridePendingTransition(R.anim.begin, R.anim.end);
    }

    @Override
    public void onFocusChange(View view, boolean flag)
    {
        View parentView = (View) view.getParent();

        if (parentView != null) {
            if (flag) {
                parentView.bringToFront();
                parentView.setBackgroundResource(R.drawable.select_bg);
            } else
                parentView.setBackgroundDrawable(null);
        }
    }

    class NetStatReceiver extends BroadcastReceiver
    {
        NetStatReceiver() {
        }

        public void onReceive(Context context, Intent intent)
        {
            try {
                if (intent.getAction().equals("android.net.conn.CONNECTIVITY_CHANGE")) {
                    if (intent.getBooleanExtra("noConnectivity", false)) {
                        
                    }

                    ConnectivityManager connectivityManager = (ConnectivityManager) context
                            .getSystemService("connectivity");

                    if (connectivityManager != null) {

                    }

                    ((TextView) findViewById(R.id.tv_ipaddr)).setText(getLocalIpAddress());
                }
            } catch (NullPointerException exception) {
                exception.printStackTrace();
            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }
    }
}
